#wrapper for teh mysql 
import pg
'''
This is not currently needed
import _mysql

class MySQL:
	def __init__(self,host,username,password,database):
		self.sql=pg.connect(host,username,password,database)
	
	def query(self,query):
		self.sql.query(query)
	
	def querya(self,query):
		self.sql.query(query)
		res=self.sql.store_result()
		arr=[]
		for i in range(res.num_rows()):
			arr.append(res.fetch_row()[0])
		return arr
	
	def querys(self,query):
		self.sql.query(query)
		res=self.sql.store_result()
		if res==None:
			return False
		else:
			return res.fetch_row()[0]
		
	def queryc(self,table,whereclause):
		res=self.querys("SELECT COUNT(*) FROM `"+table+"` WHERE "+whereclause)
		return res[0];
	
	def insertid(self):
		return self.sql.insert_id()
'''
'''
 PostgreSQL class
 class for wrapping the PostgreSQL python library in a more usable shell
'''
class PostgreSQL:
	'''
	 __init__(String,String,String,String)
	 constructor - connects to the database
	 @param database - name of the database
	 @param host - host ip of the database
	 @param username - username for connecting to the host database
	 @param password - password for connecting to the host database
	'''
	def __init__(self,database,host,username,password):
		self.sql=pg.connect(database,host,5432,None,None,username,password)
	
	'''
	 query(String)
	 Perform a query on the database which will not return any data such as an Insert or update statement
	 @param query: query to run on the database
	'''
	def query(self,query):
		self.sql.query(query)
	
	'''
	 querya(String)
	 Perform a query which will return data, as an Array
	 @param query: query to run on the database
	 @return: array of results indexed numerically
	'''
	def querya(self,query):
		self.sql.query(query)
		res=self.sql.store_result()
		arr=[]
		for i in range(res.num_rows()):
			arr.append(res.fetch_row()[0])
		return arr
	
	'''
	 querys(String)
	 perform a query which will return data, but only return the first row
	 @param query: query to run on the database
	 @return: a single entry from the query
	'''
	def querys(self,query):
		self.sql.query(query)
		res=self.sql.store_result()
		if res==None:
			return False
		else:
			return res.fetch_row()[0]
	'''
	 queryd(String)
	 perform a query which will return data, which is returned as a list of dictionaries
	 @param query: query to run on the database
	 @param: list of dictionaries where the dictionary keys are the column names and list entries are rows
	'''	
	def queryd(self,query):
		return self.sql.query(query).dictresult()
	
	'''
	 queryc(String,String)
	 count how many elements are in a table based on provided where clause
	 @param table: name of table to perform query on
	 @param whereclause: clause to limit count return
	 @return: Numeric count of how many rows were returned
	'''	
	def queryc(self,table,whereclause):
		res=self.querys("SELECT COUNT(*) FROM `"+table+"` WHERE "+whereclause)
		return res[0];
	'''
	 insertid()
	 gets the last inserted serial id
	 @return: Numeric representing last inserted id into a serial column
	'''
	def insertid(self):
		return self.sql.insert_id()